package com.xgc.erp.order.sell.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.IncomeDetail;
import com.xgc.common.visit.Visitor;
import com.xgc.common.vo.Feedback;
import com.xgc.common.vo.Grid;
import com.xgc.erp.order.sell.service.IncomeDetailService;

import java.util.Date;

//@ControllerBind(path = "/erp/order/incomeDetail")
public class IncomeDetailController extends BaseController {
    @Inject
    IncomeDetailService detailService;

    public void index(){
        String orderId = get("orderId");
        set("orderId",orderId);
        render("index.html");
    }

    /**
     * 订单明细
     */
    public void list(){
        String orderId = get("orderId");
        set("orderId",orderId);         // 页面隐藏框值填充
        String sql = Db.getSql("erp.getIncomeDetailList");
        renderJson(new Grid(Db.find(sql,orderId)));
    }

    public void add(){
        Integer orderId = getInt("orderId");
        set("incomeDetail",new IncomeDetail().setOrderId(orderId));
        render("add.html");
    }

    public void save(){
        IncomeDetail incomeDetail=getBean(IncomeDetail.class);
        incomeDetail.setDetailId(detailService.getPrimaryKey(Constan.SEQ_OUTCOME_DETAIL));
        Visitor vs = getVisitor();
        incomeDetail.setCreator(vs.getCode());
        incomeDetail.setCreateTime(new Date());
        if(!detailService.save(incomeDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("add.html");
    }

    public void edit(){
        String detailId = get("detailId");
        IncomeDetail incomeDetail = (IncomeDetail) detailService.findById(detailId);
        setAttr("incomeDetail",incomeDetail);
        render("edit.html");
    }

    public void update(){
        IncomeDetail incomeDetail=getBean(IncomeDetail.class);
        incomeDetail.setModifier(getVisitor().getCode());
        incomeDetail.setUpdateTime(new Date());
        if(!detailService.save(incomeDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("edit.html");
    }
}
